package com.xgh.offer;

/**
 * @ClassName Offer05
 * @Description
 * @Author xinggh
 * @Date 2020/6/29 9:58
 * @Version 1.0
 **/
public class Offer05 {

    /**
     * 剑指 Offer 05. 替换空格
     * 请实现一个函数，把字符串 s 中的每个空格替换成"%20"。
     * <p>
     * <p>
     * <p>
     * 示例 1：
     * <p>
     * 输入：s = "We are happy."
     * 输出："We%20are%20happy."
     * <p>
     * <p>
     * 限制：
     * <p>
     * 0 <= s 的长度 <= 10000
     */

    public String replaceSpace(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        int len = s.length();
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) == ' ') {
                stringBuilder.append("%20");
            } else {
                stringBuilder.append(s.charAt(i));
            }
        }
        return stringBuilder.toString();
    }
}
